#include "StdAfx.h"
#include "Dictionarie.h"
#include "String.h"
#include "Factory.h"

using namespace Torrent::Bencode;

Dictionarie::Dictionarie(std::istream& stream)
{
    stream.get();
    while (stream.good()) 
    {
        if (stream.peek() == 'e')
        {
            stream.get();
            return;
        }

        String *pString = new String(stream);
        //std::string str(pString->getValue());
        
        _map[pString] = Factory::create(stream);
    }
}

Dictionarie::~Dictionarie(void)
{
}

void Dictionarie::print( std::ostream& stream, bool debug /*= false*/ )
{
    stream << "d";

    std::map<MapKey,MapElement>::iterator it;
    for (it = _map.begin(); it != _map.end(); ++it)
    {
        it->first->print(stream,debug);
        it->second->print(stream,debug);
    }

    stream << "e";
}